//
//  ViewController.swift
//  001.计步器CMPedometer的使用（获取用户步数、距离、速度等）
//
//  Created by Gao on 17/2/16.
//  Copyright © 2017年 Odm. All rights reserved.
//

import UIKit
import CoreMotion

class ViewController: UIViewController {
    
    
    /**
     首先我们要在 info.plist 文件中加入访问用户健康和运动信息的相关描述。
     http://www.hangge.com/blog_uploads/201702/201702081616076710.png
     
     <key>NSMotionUsageDescription</key>
     <string>计步器需要获得您的运动信息</string>
     <key>CFBundleDevelopmentRegion</key>
     
     需要真机调试
     
     */

    
    
    //用于显示实时信息 
    @IBOutlet weak var textView: UITextView!
    
    //计步器对象 需要导入CoreMotion框架
    let pedometer = CMPedometer()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //开始计步器更新
        startPedometerUpdates()
        
//        //开始获取高度计数据更新
//        startRelativeAltitudeUpdates()
        
    }

    // 开始获取步数计数据
    func startPedometerUpdates() {
        
        //判断设备支持情况
        guard CMPedometer.isStepCountingAvailable() else {
            self.textView.text = "\n当前设备不支持获取步数\n"
            return
        }
        
        
        //获取今天凌晨时间
        let cal = Calendar.current
        var comps = cal.dateComponents([.year, .month, .day], from: Date())
        comps.hour = 0
        comps.minute = 0
        comps.second = 0
        let midnightOfToday = cal.date(from: comps)!
        
        
        //初始化并开始实时获取数据
        self.pedometer.startUpdates (from: midnightOfToday, withHandler: { pedometerData, error in
            //错误处理
            guard error == nil else {
                print(error!)
                return
            }
            
            //获取各个数据
            var text = "---今日运动数据---\n"
            if let numberOfSteps = pedometerData?.numberOfSteps {
                text += "步数: \(numberOfSteps)\n"
            }
            if let distance = pedometerData?.distance {
                text += "距离: \(distance)\n"
            }
            if let floorsAscended = pedometerData?.floorsAscended {
                text += "上楼: \(floorsAscended)\n"
            }
            if let floorsDescended = pedometerData?.floorsDescended {
                text += "下楼: \(floorsDescended)\n"
            }
            if let currentPace = pedometerData?.currentPace {
                text += "速度: \(currentPace)m/s\n"
            }
            if let currentCadence = pedometerData?.currentCadence {
                text += "速度: \(currentCadence)步/秒\n"
            }
            
            //在线程中更新文本框数据
            DispatchQueue.main.async{
                self.textView.text = text
            }
        })
        
        
    }
    

    //高度计对象
    let altimeter = CMAltimeter()
    // 开始获取高度计数据
    func startRelativeAltitudeUpdates() {
        //判断设备支持情况
        guard CMAltimeter.isRelativeAltitudeAvailable() else {
            self.textView.text = "\n当前设备不支持获取高度\n"
            return
        }
        
        //初始化并开始实时获取数据
        let queue = OperationQueue.current
        self.altimeter.startRelativeAltitudeUpdates(to: queue!, withHandler: {
            (altitudeData, error) in
            //错误处理
            guard error == nil else {
                print(error!)
                return
            }
            
            //获取各个数据
            var text = "---高度计数据---\n"
            text += "高度变化: \(altitudeData!.relativeAltitude) 米\n"
            text += "压力: \(altitudeData!.pressure) kPa\n"
            self.textView.text = text
            
        })
    }
    

}

